<template>
<!-- 导航栏 -->
  <div class="type-nav">
    <div
      class="container"
      @mouseleave="$route.path != '/home' ? (show = false) : (show = true)"
    >
      <h2 class="all" @mouseenter="show = true">全部商品分类</h2>
      <nav class="nav">
        <a href="###">服装城</a>
        <a href="###">美妆馆</a>
        <a href="###">尚品汇超市</a>
        <a href="###">全球购</a>
        <a href="###">闪购</a>
        <a href="###">团购</a>
        <a href="###">有趣</a>
        <a href="###">秒杀</a>
      </nav>
      <!--过渡动画 -->
      <transition name='sort'>
        <div class="sort" v-show="show">
          <!-- 将点击事件绑定给需要跳转标签们的父元素 -->
          <div class="all-sort-list2" @click="goSearch">
            <!-- 一级菜单 -->
            <div
              class="item"
              v-for="(c1, index) in categoryList"
              :key="c1.categoryId"
              :class="{ cur: currentIndex == index }"
              @mouseenter="changeIndex(index)"
              @mouseleave="leave"
            >
              <h3>
                <a
                  :data-categoryName="c1.categoryName"
                  :data-categoryId1="c1.categoryId"
                  >{{ c1.categoryName }}</a
                >
              </h3>
              <div class="item-list clearfix">
                <div class="subitem">
                  <!-- 为二级菜单 -->
                  <dl
                    class="fore"
                    v-for="c2 in c1.categoryChild"
                    :key="c2.categoryId"
                  >
                    <dt>
                      <a
                        :data-categoryName="c2.categoryName"
                        :data-categoryId2="c2.categoryId"
                        >{{ c2.categoryName }}</a
                      >
                    </dt>
                    <dd>
                      <!-- 三级菜单 -->
                      <em v-for="c3 in c2.categoryChild" :key="c3.categoryId">
                        <a
                          :data-categoryName="c3.categoryName"
                          :data-categoryId3="c3.categoryId"
                          >{{ c3.categoryName }}</a
                        >
                      </em>
                    </dd>
                  </dl>
                </div>
              </div>
            </div>
          </div>
        </div>
      </transition>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
//引入lodash里的节流方法
import throttle from "lodash/throttle";
export default {
  name: "TypeNav",
  data() {
    return {
      currentIndex: -1,
      show: true,
    };
  },
  //组件挂载完毕可以向服务器发起请求
  mounted() {
    //组件挂载时，如果路由不是home就将其隐藏
    if (this.$route.path != "/home") {
      this.show = false;
    }
  },
  computed: {
    ...mapState({
      //对象的写法右侧需要一个函数，当执行这个计算属性的时候右侧的函数会立即执行一次
      //注入第一个参数state就是大仓库中的数据
      categoryList: (state) => state.home.categoryList,
    }),
  },
  methods: {
    //鼠标移入修改currentIndex属性
    // 这里throttle的回调函数别用箭头函数，可能会出现this问题
    changeIndex: throttle(function (index) {
      this.currentIndex = index;
    }, 50),
    leave() {
      this.currentIndex = -1;
    },
    //路由的跳转
    //event为事件对象
    goSearch(event) {
      //事件身上的event.target.dataset里面保存有自定义属性
      let { categoryname, categoryid1, categoryid2, categoryid3 } =
        event.target.dataset;
      //有categoryname属性的一定为a标签
      if (categoryname) {
        let location = { name: "search" };
        let query = { categoryName: categoryname };
        //根据ategoryid判断是几级分类
        if (categoryid1) {
          query.category1Id = categoryid1;
        } else if (categoryid2) {
          query.category2Id = categoryid2;
        } else if (categoryid3) {
          query.category3Id = categoryid3;
        }
        //合并参数，如果有params参数也要跟着传过去
        location.params = this.$route.params
        location.query = query;
        this.$router.push(location);
      }
    },
  },
};
</script>

<style scoped lang="less">
.type-nav {
  border-bottom: 2px solid #e1251b;

  .container {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    position: relative;

    .all {
      width: 210px;
      height: 45px;
      background-color: #e1251b;
      line-height: 45px;
      text-align: center;
      color: #fff;
      font-size: 14px;
      font-weight: bold;
    }

    .nav {
      a {
        height: 45px;
        margin: 0 22px;
        line-height: 45px;
        font-size: 16px;
        color: #333;
      }
    }

    .sort {
      position: absolute;
      left: 0;
      top: 45px;
      width: 210px;
      height: 461px;
      position: absolute;
      background: #fafafa;
      z-index: 999;

      .all-sort-list2 {
        height: 450px;
        overflow: auto;
        &::-webkit-scrollbar {
          width: 2px;
        }
        &::-webkit-scrollbar-thumb {
          background-color: rgb(156, 135, 135);
        }
        .cur {
          background-color: skyblue;
        }
        .item {
          h3 {
            line-height: 30px;
            font-size: 14px;
            font-weight: 400;
            overflow: hidden;
            padding: 0 20px;
            margin: 0;

            a {
              color: #333;
            }
          }

          .item-list {
            display: none;
            position: absolute;
            width: 734px;
            min-height: 460px;
            background: #f7f7f7;
            left: 210px;
            border: 1px solid #ddd;
            top: 0;
            z-index: 9999 !important;

            .subitem {
              float: left;
              width: 650px;
              padding: 0 4px 0 8px;

              dl {
                border-top: 1px solid #eee;
                padding: 6px 0;
                overflow: hidden;
                zoom: 1;

                &.fore {
                  border-top: 0;
                }

                dt {
                  float: left;
                  width: 54px;
                  line-height: 22px;
                  text-align: right;
                  padding: 3px 6px 0 0;
                  font-weight: 700;
                }

                dd {
                  float: left;
                  width: 415px;
                  padding: 3px 0 0;
                  overflow: hidden;

                  em {
                    float: left;
                    height: 14px;
                    line-height: 14px;
                    padding: 0 8px;
                    margin-top: 5px;
                    border-left: 1px solid #ccc;
                  }
                }
              }
            }
          }

          &:hover {
            .item-list {
              display: block;
            }
          }
        }
      }
    }
    //过渡动画的样式
    //开始状态
    .sort-enter{
      height: 0px;
    }
    //结束状态
    .sort-enter-to{
      height: 461px;
    }
    // .sort-leave{
    //   height: 461px;
    // }
    // .sort-leave-to{
    //   height: 0px;
    // }
    //定义动画的时间
    .sort-enter-active{
      transition: all .5s linear;
      overflow: hidden;//是文字跟随动画显示
    }
    // .sort-leave-active{
    //   transition: all .5s linear;
    //   overflow: hidden;
    // }
  }
}
</style>